package com.mocchira.music.radio;

import java.net.URLEncoder;
import java.util.Locale;

public class AmazonAssociateUtil {
    private static final String TYPE_MUSIC = "music";
    private static final String TYPE_MP3 = "digital-music";
    private static final String JP_TEMPLATE = "http://www.amazon.co.jp/gp/search?ie=UTF8&keywords=${key}&tag=mocchira-22&index=${type}&linkCode=ur2&camp=247&creative=1211";
    private static final String US_TEMPLATE = "http://www.amazon.com/gp/search?ie=UTF8&keywords=${key}&tag=mocchira-20&index=${type}&linkCode=ur2&camp=1789&creative=9325";
    private static final String CN_TEMPLATE = "http://www.amazon.cn/s/ref=nb_sb_noss?__mk_zh_CN=%E4%BA%9A%E9%A9%AC%E9%80%8A%E7%BD%91%E7%AB%99&url=search-alias%3Dmusic&field-keywords=${key}&x=0&y=0&tag=mocchira-23&linkCode=ur2&camp=536&creative=3200&nodeid=754386051";
    private static final String UK_TEMPLATE = "http://www.amazon.co.uk/s/ref=nb_sb_noss?url=search-alias%3D${type}&field-keywords=${key}&x=0&y=0&node=520920&tag=mocchira07-21&linkCode=ur2&camp=1634&creative=6738";
    private static final String DE_TEMPLATE = "http://www.amazon.de/s/ref=nb_sb_noss&url=search-alias%3D${type}&field-keywords=${key}&x=0&y=0&node=77258031&tag=mocchira0d-21&linkCode=ur2&camp=1638&creative=6742";
    private static final String FR_TEMPLATE = "http://www.amazon.fr/s/ref=nb_sb_ss_i_0_9?__mk_fr_FR=%C5M%C5Z%D5%D1&url=search-alias%3D${type}&field-keywords=${key}&x=0&y=0&node=301062&tag=mocchira03-21&linkCode=ur2&camp=1642&creative=6746";
    private static final String CA_TEMPLATE = "http://www.amazon.ca/s/ref=nb_sb_noss?url=search-alias%3Dca-music-tree&field-keywords=${key}&x=0&y=0&node=916514&tag=mocchira0b-20&linkCode=ur2&camp=15121&creative=330641";
    private static final String IT_TEMPLATE = "http://www.amazon.it/s/ref=nb_sb_noss?__mk_it_IT=%C5M%C5Z%D5%D1&url=search-alias%3Dpopular&field-keywords=${key}&x=0&y=0&node=412600031&tag=mocchira06-21&linkCode=ur2&camp=3370&creative=23322";

    public static String getMusicLinkURL(Locale local, String artist, String album) {
        String enc = "";
        try {
            enc = URLEncoder.encode(artist + " " + album, "UTF-8");
        } catch (Exception ex) {}
        String ret = "";
        if (local.equals(Locale.JAPAN) || local.equals(Locale.JAPANESE)) {
            ret = JP_TEMPLATE.replace("${key}", enc);
            ret = ret.replace("${type}", TYPE_MUSIC);
        } else if (local.equals(Locale.CANADA) || local.equals(Locale.CANADA_FRENCH)) {
            ret = CA_TEMPLATE.replace("${key}", enc);
        } else if (local.equals(Locale.CHINA) || local.equals(Locale.CHINESE)) {
            ret = CN_TEMPLATE.replace("${key}", enc);
        } else if (local.equals(Locale.ENGLISH)) {
            ret = UK_TEMPLATE.replace("${key}", enc);
            ret = ret.replace("${type}", TYPE_MUSIC);
        } else if (local.equals(Locale.FRANCE) || local.equals(Locale.FRENCH)) {
            ret = FR_TEMPLATE.replace("${key}", enc);
            ret = ret.replace("${type}", TYPE_MUSIC);
        } else if (local.equals(Locale.GERMAN) || local.equals(Locale.GERMANY)) {
            ret = DE_TEMPLATE.replace("${key}", enc);
            ret = ret.replace("${type}", TYPE_MUSIC);
        } else if (local.equals(Locale.ITALIAN) || local.equals(Locale.ITALY)) {
            ret = IT_TEMPLATE.replace("${key}", enc);
        } else {
            ret = US_TEMPLATE.replace("${key}", enc);
            ret = ret.replace("${type}", TYPE_MUSIC);
        }
        return ret;
    }
    public static String getMP3LinkURL(Locale local, String artist, String track) {
        String enc = "";
        try {
            enc = URLEncoder.encode(artist + " " + track, "UTF-8");
        } catch (Exception ex) {}
        String ret = "";
        if (local.equals(Locale.JAPAN) || local.equals(Locale.JAPANESE)) {
            ret = JP_TEMPLATE.replace("${key}", enc);
            ret = ret.replace("${type}", TYPE_MP3);
        } else if (local.equals(Locale.CANADA) || local.equals(Locale.CANADA_FRENCH)) {
            ret = CA_TEMPLATE.replace("${key}", enc);
        } else if (local.equals(Locale.CHINA) || local.equals(Locale.CHINESE)) {
            ret = CN_TEMPLATE.replace("${key}", enc);
        } else if (local.equals(Locale.ENGLISH)) {
            ret = UK_TEMPLATE.replace("${key}", enc);
            ret = ret.replace("${type}", TYPE_MP3);
        } else if (local.equals(Locale.FRANCE) || local.equals(Locale.FRENCH)) {
            ret = FR_TEMPLATE.replace("${key}", enc);
            ret = ret.replace("${type}", TYPE_MP3);
        } else if (local.equals(Locale.GERMAN) || local.equals(Locale.GERMANY)) {
            ret = DE_TEMPLATE.replace("${key}", enc);
            ret = ret.replace("${type}", TYPE_MP3);
        } else if (local.equals(Locale.ITALIAN) || local.equals(Locale.ITALY)) {
            ret = IT_TEMPLATE.replace("${key}", enc);
        } else {
            ret = US_TEMPLATE.replace("${key}", enc);
            ret = ret.replace("${type}", TYPE_MP3);
        }
        return ret;
    }
}
